package logUtils

import (
	"bytes"
	"fmt"
	"os"
	"time"
	"utilsx/constant"
)

var _buffer *bytes.Buffer

func init() {
	getBuffer()
}

func getBuffer() *bytes.Buffer {
	if _buffer == nil {
		_buffer = &bytes.Buffer{}
		return _buffer
	}
	return _buffer
}

func ShowAllColor() {
	for b := 40; b <= 47; b++ { // 背景色彩 = 40-47
		for f := 30; f <= 37; f++ { // 前景色彩 = 30-37
			for d := range []int{0, 1, 4, 5, 7, 8} { // 显示方式 = 0,1,4,5,7,8
				fmt.Printf(" %c[%d;%d;%dm%s(f=%d,b=%d,d=%d)%c[0m ", 0x1B, d, b, f, "", f, b, d, 0x1B)
			}
			fmt.Println("")
		}
		fmt.Println("")
	}
}

func LogPrintln(level int, str ...any) {
	getPrintFB(level, 2)(time.Now().Format(constant.TimeFormat))
	fmt.Fprintf(os.Stdout, " ")
	getPrintFB(level, 7)(str...)
	fmt.Println()

}
func Log(level int, str ...any) {
	getPrintFB(level, 0)(str...)

}
func LogLn(level int, str ...any) {
	getPrintFB(level, 0)(str...)
	fmt.Println()
}
func LogLnBack(level, f int, str ...any) {
	getPrintFB(level, f)(str...)
	fmt.Println()
}
func getPrintFB(color, f int) func(info ...any) {
	return func(info ...any) {
		_buffer.Reset() //清空緩存
		for i := range info {
			_buffer.WriteString(fmt.Sprintf("%v", info[i]))
		}
		fmt.Fprintf(os.Stdout, "\033[1;%d;%dm%v\033[0m", 30+color%7, 40+f%7, _buffer.String())
	}
}
